package com.changyinBus.web.controller.system;

import com.changyinBus.common.annotation.Log;
import com.changyinBus.common.core.controller.BaseController;
import com.changyinBus.common.core.domain.AjaxResult;
import com.changyinBus.common.core.domain.entity.SysProdCate;
import com.changyinBus.common.enums.BusinessType;
import com.changyinBus.common.utils.StringUtils;
import com.changyinBus.system.service.ISysProdCateService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 产品分类管理
 */
@RestController
@RequestMapping("/system/prodCate")
public class SysProdCateContorller extends BaseController {
    @Resource
    private ISysProdCateService sysProdCateService;

    /**
     * 获取产品分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:prodCate:list')")
    @GetMapping("/list")
    public AjaxResult list(SysProdCate cate) {
        cate.setParentId(-1);
        List<SysProdCate> depts = sysProdCateService.selectProdCateList(cate);
        return success(depts);
    }

    /**
     * 根据产品分类编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:prodCate:query')")
    @GetMapping(value = "/{cateId}")
    public AjaxResult getInfo(@PathVariable Long cateId) {
        return success(sysProdCateService.selectProdCateById(cateId));
    }

    /**
     * 新增产品分类
     */
    @PreAuthorize("@ss.hasPermi('system:prodCate:add')")
    @Log(title = "产品分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysProdCate cate) {
        cate.setCreateBy(getUsername());
        return success(sysProdCateService.insertProdCate(cate));
    }

    /**
     * 修改产品分类
     */
    @PreAuthorize("@ss.hasPermi('system:prodCate:edit')")
    @Log(title = "产品分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysProdCate cate) {
        if (cate.getParentId() == cate.getProdCateId()) {
            return error("修改产品分类'" + cate.getProdCateName() + "'失败，上级分类不能是自己");
        }
        cate.setUpdateBy(getUsername());
        return toAjax(sysProdCateService.updateProdCate(cate));
    }

    /**
     * 查询产品分类列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:prodCate:list')")
    @PostMapping("/list/exclude")
    public AjaxResult excludeChild(@RequestBody SysProdCate cate) {
        SysProdCate param = new SysProdCate();
        param.setTenantId(cate.getTenantId());
        List<SysProdCate> depts = sysProdCateService.selectProdCateList(param);
        depts.removeIf(d -> d.getProdCateId() == cate.getProdCateId() || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), cate.getProdCateId() + ""));
        return success(depts);
    }

    /**
     * 删除产品分类
     */
    @PreAuthorize("@ss.hasPermi('system:prodCate:remove')")
    @Log(title = "产品分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cateId}")
    public AjaxResult remove(@PathVariable Long cateId) {
        if (sysProdCateService.hasChildByProdCateId(cateId) > 0) {
            return warn("存在下级分类,不允许删除");
        }
        return toAjax(sysProdCateService.deleteProdCateById(cateId));
    }
}
